<!DOCTYPE html>

<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width">
	<title>Common Modules Source: control/ScaleLineH.js</title>

	<!--[if lt IE 9]>
	<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">

	<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">

</head>

<body>

<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
	<div class="navbar-header">
		<a class="navbar-brand" href="index.html">Common Modules</a>
		<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
        </button>
	</div>
	<div class="navbar-collapse collapse" id="topNavigation">
		<ul class="nav navbar-nav">
			
			<li class="dropdown">
				<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="Base.html">Base</a></li><li><a href="Event.html">Event</a></li><li><a href="Html.html">Html</a></li><li><a href="Internal.html">Internal</a></li><li><a href="ol.Overlay.Popup.html">ol.Overlay.Popup</a></li>
				</ul>
			</li>
			
			<li class="dropdown">
				<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="global.html#addClass">addClass</a></li><li><a href="global.html#addListener">addListener</a></li><li><a href="global.html#arrayRemove">arrayRemove</a></li><li><a href="global.html#clear">clear</a></li><li><a href="global.html#create">create</a></li><li><a href="global.html#createHidden">createHidden</a></li><li><a href="global.html#empty">empty</a></li><li><a href="global.html#EventType">EventType</a></li><li><a href="global.html#findListener">findListener</a></li><li><a href="global.html#firstUpperToCase">firstUpperToCase</a></li><li><a href="global.html#get">get</a></li><li><a href="global.html#getChildByTagName">getChildByTagName</a></li><li><a href="global.html#getClass">getClass</a></li><li><a href="global.html#getDomEventKey">getDomEventKey</a></li><li><a href="global.html#getElementsByClassName">getElementsByClassName</a></li><li><a href="global.html#getListeners">getListeners</a></li><li><a href="global.html#getrandom">getrandom</a></li><li><a href="global.html#getStyle">getStyle</a></li><li><a href="global.html#getuuid">getuuid</a></li><li><a href="global.html#hasClass">hasClass</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isString">isString</a></li><li><a href="global.html#layerConfigDefautls">layerConfigDefautls</a></li><li><a href="global.html#listen">listen</a></li><li><a href="global.html#listenOnce">listenOnce</a></li><li><a href="global.html#logo">logo</a></li><li><a href="global.html#OL_Style_Type">OL_Style_Type</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeClass">removeClass</a></li><li><a href="global.html#removeListener">removeListener</a></li><li><a href="global.html#removeListeners">removeListeners</a></li><li><a href="global.html#setClass">setClass</a></li><li><a href="global.html#splitWords">splitWords</a></li><li><a href="global.html#stamp">stamp</a></li><li><a href="global.html#toBack">toBack</a></li><li><a href="global.html#toFront">toFront</a></li><li><a href="global.html#trim">trim</a></li><li><a href="global.html#unListen">unListen</a></li><li><a href="global.html#unlistenAll">unlistenAll</a></li><li><a href="global.html#unListenByKey">unListenByKey</a></li><li><a href="global.html#upperFirstChart">upperFirstChart</a></li>
				</ul>
			</li>
			
		</ul>
        
            <div class="col-sm-3 col-md-3">
                <form class="navbar-form" role="search">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
                        <div class="input-group-btn">
                            <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
                        </div>
                    </div>
                </form>
            </div>
        
	</div>

</div>
</div>


<div class="container" id="toc-content">
<div class="row">

	
	<div class="col-md-12">
	
		<div id="main">
			

		<h1 class="page-title">Source: control/ScaleLineH.js</h1>
    
<section>
    <article>
        <pre
            class="sunlight-highlight-javascript linenums">/**
 * Created by FDD on 2017/10/11.
 * @desc 比例尺控件
 */
import { ol } from '../constants'
import '../asset/scss/ScaleLineH.scss'
import {BASE_CLASS_NAME, UNITS} from '../constants'
import * as htmlUtils from '../utils/domUtils'
import * as Events from '../event/Events'
ol.control.ScaleLineH = function (options = {}) {
  const className = options.className !== undefined ? options.className : 'gmap-scale-line-control'

  /**
   * @private
   * @type {Element}
   */
  this.element_ = htmlUtils.create('div', (className + ' ' + BASE_CLASS_NAME.CLASS_UNSELECTABLE))

  /**
   * @private
   * @type {Element}
   */
  this.innerElement_ = htmlUtils.create('div', (className + '-inner'), this.element_)

  /**
   * @private
   * @type {?olx.ViewState}
   */
  this.viewState_ = null

  /**
   * @private
   * @type {number}
   */
  this.minWidth_ = options.minWidth !== undefined ? options.minWidth : 64

  /**
   * @private
   * @type {boolean}
   */
  this.renderedVisible_ = false

  /**
   * @private
   * @type {number|undefined}
   */
  this.renderedWidth_ = undefined

  /**
   * @private
   * @type {string}
   */
  this.renderedHTML_ = ''

  const render = options.render ? options.render : ol.control.ScaleLineH.render

  ol.control.Control.call(this, {
    element: this.element_,
    render: render,
    target: options.target
  })
  Events.listen(this, 'change:' + ol.control.ScaleLineH.Property_.UNITS, this.handleUnitsChanged_, this)
  this.setUnits((options.units) || ol.control.ScaleLineH.ScaleLineUnits.METRIC)
}

ol.inherits(ol.control.ScaleLineH, ol.control.Control)

/**
 * @const
 * @type {Array.&lt;number>}
 */
ol.control.ScaleLineH.LEADING_DIGITS = [1, 2, 5]

/**
 * 内置单位
 * @type {{DEGREES: string, IMPERIAL: string, NAUTICAL: string, METRIC: string, CHINESEMETRIC: string, US: string}}
 */
ol.control.ScaleLineH.ScaleLineUnits = {
  DEGREES: 'degrees',
  IMPERIAL: 'imperial',
  NAUTICAL: 'nautical',
  METRIC: 'metric',
  CHINESEMETRIC: 'metric_cn',
  US: 'us'
}

/**
 * 返回比例尺使用的单位
 * @returns {*}
 */
ol.control.ScaleLineH.prototype.getUnits = function () {
  return (this.get(ol.control.ScaleLineH.Property_.UNITS))
}

/**
 * 更新element
 * @param mapEvent
 */
ol.control.ScaleLineH.render = function (mapEvent) {
  let frameState = mapEvent.frameState
  if (!frameState) {
    this.viewState_ = null
  } else {
    this.viewState_ = frameState.viewState
  }
  this.updateElement_()
}

/**
 * 处理单位变化事件
 * @private
 */
ol.control.ScaleLineH.prototype.handleUnitsChanged_ = function () {
  this.updateElement_()
}

/**
 * 设置使用的单位
 * @param units
 */
ol.control.ScaleLineH.prototype.setUnits = function (units) {
  this.set(ol.control.ScaleLineH.Property_.UNITS, units)
}

/**
 * 更新element
 * @private
 */
ol.control.ScaleLineH.prototype.updateElement_ = function () {
  let viewState = this.viewState_
  if (!viewState) {
    if (this.renderedVisible_) {
      this.element_.style.display = 'none'
      this.renderedVisible_ = false
    }
    return
  }
  let [center, projection] = [viewState.center, viewState.projection]
  let units = this.getUnits()
  let pointResolutionUnits = units === ol.control.ScaleLineH.ScaleLineUnits.DEGREES ? UNITS.DEGREES : UNITS.METERS
  let pointResolution = ol.proj.getPointResolution(projection, viewState.resolution, center, pointResolutionUnits)
  let nominalCount = this.minWidth_ * pointResolution
  let suffix = ''
  if (units === ol.control.ScaleLineH.ScaleLineUnits.DEGREES) {
    let metersPerDegree = ol.proj.METERS_PER_UNIT[UNITS.DEGREES]
    if (projection.getUnits() === UNITS.DEGREES) {
      nominalCount *= metersPerDegree
    } else {
      pointResolution /= metersPerDegree
    }
    if (nominalCount &lt; metersPerDegree / 60) {
      suffix = '\u2033' // seconds
      pointResolution *= 3600
    } else if (nominalCount &lt; metersPerDegree) {
      suffix = '\u2032' // minutes
      pointResolution *= 60
    } else {
      suffix = '\u00b0' // degrees
    }
  } else if (units === ol.control.ScaleLineH.ScaleLineUnits.IMPERIAL) {
    if (nominalCount &lt; 0.9144) {
      suffix = 'in'
      pointResolution /= 0.0254
    } else if (nominalCount &lt; 1609.344) {
      suffix = 'ft'
      pointResolution /= 0.3048
    } else {
      suffix = 'mi'
      pointResolution /= 1609.344
    }
  } else if (units === ol.control.ScaleLineH.ScaleLineUnits.NAUTICAL) {
    pointResolution /= 1852
    suffix = 'nm'
  } else if (units === ol.control.ScaleLineH.ScaleLineUnits.METRIC) {
    if (nominalCount &lt; 0.001) {
      suffix = 'μm'
      pointResolution *= 1000000
    } else if (nominalCount &lt; 1) {
      suffix = 'mm'
      pointResolution *= 1000
    } else if (nominalCount &lt; 1000) {
      suffix = 'm'
    } else {
      suffix = 'km'
      pointResolution /= 1000
    }
  } else if (units === ol.control.ScaleLineH.ScaleLineUnits.US) {
    if (nominalCount &lt; 0.9144) {
      suffix = 'in'
      pointResolution *= 39.37
    } else if (nominalCount &lt; 1609.344) {
      suffix = 'ft'
      pointResolution /= 0.30480061
    } else {
      suffix = 'mi'
      pointResolution /= 1609.3472
    }
  } else if (units === ol.control.ScaleLineH.ScaleLineUnits.CHINESEMETRIC) {
    if (nominalCount &lt; 0.001) {
      suffix = '微米'
      pointResolution *= 1000000
    } else if (nominalCount &lt; 1) {
      suffix = '毫米'
      pointResolution *= 1000
    } else if (nominalCount &lt; 1000) {
      suffix = '米'
    } else {
      suffix = '千米'
      pointResolution /= 1000
    }
  } else {
    ol.asserts.assert(false, 33) // Invalid units
  }

  let i = 3 * Math.floor(
      Math.log(this.minWidth_ * pointResolution) / Math.log(10))
  let [count, width] = []
  while (true) {
    count = ol.control.ScaleLineH.LEADING_DIGITS[((i % 3) + 3) % 3] * Math.pow(10, Math.floor(i / 3))
    width = Math.round(count / pointResolution)
    if (isNaN(width)) {
      this.element_.style.display = 'none'
      this.renderedVisible_ = false
      return
    } else if (width >= this.minWidth_) {
      break
    }
    ++i
  }
  let html = count + ' ' + suffix
  if (this.renderedHTML_ !== html) {
    this.innerElement_.innerHTML = html
    this.renderedHTML_ = html
  }
  if (this.renderedWidth_ !== width) {
    this.innerElement_.style.width = width + 'px'
    this.renderedWidth_ = width
  }
  if (!this.renderedVisible_) {
    this.element_.style.display = ''
    this.renderedVisible_ = true
  }
}

/**
 * @enum {string}
 * @private
 */
ol.control.ScaleLineH.Property_ = {
  UNITS: 'units'
}

const olControlScaleLine = ol.control.ScaleLineH
export default olControlScaleLine
</pre>
    </article>
</section>





		</div>
	</div>

	<div class="clearfix"></div>

	

</div>
</div>


    <div class="modal fade" id="searchResults">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Search results</h4>
          </div>
          <div class="modal-body"></div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div>


<footer>


	<span class="copyright">
	https://lzw.me.
	</span>

<span class="jsdoc-message">
	Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
	
		on Sun Aug 12th 2018
	
	using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>

<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>

    <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>


<script>
$( function () {
	$( "[id*='$']" ).each( function () {
		var $this = $( this );

		$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
	} );

	$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
		var $this = $( this );

		var example = $this.find( "code" );
		exampleText = example.html();
		var lang = /{@lang (.*?)}/.exec( exampleText );
		if ( lang && lang[1] ) {
			exampleText = exampleText.replace( lang[0], "" );
			example.html( exampleText );
			lang = lang[1];
		} else {
			var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
			lang = langClassMatch ? langClassMatch[1] : "javascript";
		}

		if ( lang ) {

			$this
			.addClass( "sunlight-highlight-" + lang )
			.addClass( "linenums" )
			.html( example.html() );

		}
	} );

	Sunlight.highlightAll( {
		lineNumbers : true,
		showMenu : true,
		enableDoclinks : true
	} );

	$.catchAnchorLinks( {
        navbarOffset: 10
	} );
	$( "#toc" ).toc( {
		anchorName  : function ( i, heading, prefix ) {
			return $( heading ).attr( "id" ) || ( prefix + i );
		},
		selectors   : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
		showAndHide : false,
		smoothScrolling: true
	} );

	$( "#main span[id^='toc']" ).addClass( "toc-shim" );
	$( '.dropdown-toggle' ).dropdown();

    $( "table" ).each( function () {
      var $this = $( this );
      $this.addClass('table');
    } );

} );
</script>



<!--Navigation and Symbol Display-->


<!--Google Analytics-->



    <script type="text/javascript">
        $(document).ready(function() {
            SearcherDisplay.init();
        });
    </script>


</body>
</html>
